﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WeShare.WeiXin.Common;
using WeShare.WeiXin.Common.EnumKey;
using WeShare.WeiXin.Mp;
using WeShare.WeiXin.Mp.JsSdk;
using WeShare.WeiXin.Mp.JsSdk.Entity;
using WeShare.WeiXin.Mp.MsgEntity;
using WeShare.WeiXin.Pay;
namespace WebApp.Controllers
{
    [Route("api/[controller]/[action]")]
    public class WxController : ControllerBase
    {
        List<EnterParamConfig> configs = new List<EnterParamConfig>
        {
            new EnterParamConfig
            {
                appid = "wx891ee9903ba8d74e",
                EncodingAESKey = "nKh7i1tBlQoFFp3bBX99e536VsM2tX4fIjjVFqKiKt3",
                token = "weixin",
                IsRecordLastTime = true
            },
            new EnterParamConfig
            {
                appid = "wxd7008116979a800d",
                token = "weixin",
                IsRecordLastTime = true
            }
        };
        [Route("{appid}")]
        public async Task<ActionResult> MsgHandler(string appid)
        {
            Console.WriteLine(appid);
            var param = configs.First(c => c.appid == appid);
            var res = await HttpContext.RegisterAsync<MsgHandler>(param);
            return new ContentResult { Content = res };
        }

        public WxJsParam GetJsParam(string url, bool debug)
        {
            return JsApi.GetJsParam(url, debug);
        }

        public List<AddCardParam> GetCardParams(string[] cardid)
        {
            return JsApi.GetAddCardParams(cardid.Select(s => new CardExtInfo { CardId = s }).ToList());
        }
        public ChooseCardParam GetChooseCardParam()
        {
            return JsApi.GetChooseCardParam();
        }

        [HttpPost]
        public string RefundNotify()
        {
            return HttpContext.GetRefundNofity(notifyData => { return true; });
        }
        [HttpPost]
        public string ScanPayNotify()
        {
            return HttpContext.GetScanPayNotify(req =>
            {
                var unife = PayApi.UnifiedOrder(new UnifiedReq
                {
                    notify_url = "http://www.baidu.com",
                    body = "ceshi",
                    out_trade_no = req.product_id,
                    openid = req.openid,
                    total_fee = 1,
                    spbill_create_ip = "127.0.0.1",
                    trade_type = TradeType.NATIVE
                });
                return unife.prepay_id;
            });
        }
    }
}